//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Expressions
{
    
    class AnchoredColumnReference: ColumnReference
    {
        public AnchoredColumnReference(Column column, bool isLocked)
        {
            Contract.Requires(column != null);
            _anchor = column;
            _isLocked = isLocked;
        }
        internal override DimensionReference<Row, Column> SwapDimension()
        {
            return new AnchoredRowReference(this.Worksheet.Rows[Anchor.Index], IsLocked);
        }
        internal override string GetText(TextContext context)
        {
            return Label;
        }
        private readonly Column _anchor;

        public override Column Anchor
        {
            get { return _anchor; }
        }
        public override Worksheet Worksheet
        {
            get { return _anchor.Worksheet; }
        }
	
        private readonly bool _isLocked;
        public override bool IsLocked
        {
            get { return _isLocked; }
        }

        internal override ColumnReference Projected(Worksheet targetWorksheet)
        {
            return new AnchoredColumnReference(targetWorksheet.Columns[this.Anchor.Index], this.IsLocked);
        }

        internal override ColumnReference Offset(CellRelativeAddress address)
        {
            if (IsLocked || address.Column == 0)
                return this;
            return new AnchoredColumnReference(Worksheet.Columns[Anchor.Index + address.Column], IsLocked);
            
        }

        internal override Column GetColumn(EvaluationContext context)
        {
            if (IsLocked || context.Offset.Column == 0)
                return Anchor;
            return Worksheet.Columns[Anchor.Index + context.Offset.Column];
        }

        public override string Label
        {
            get { string result = "";
                if (IsLocked)
                    result = "$";
                result += Anchor.Label;
                return result;
            }
        }

    }
}
